package com.noahplat.fourg.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * netty监听端口服务启动类
 */
@Component
public class NettyStart {
    @Resource
    private NettyHandler nettyHandler;
    private EventLoopGroup bossGroup = new NioEventLoopGroup();
    private EventLoopGroup workGroup = new NioEventLoopGroup();

    /**
     * 启动netty服务
     * @throws InterruptedException
     */
    @PostConstruct
    public void start() throws InterruptedException {
        ServerBootstrap b=new ServerBootstrap();
        b.group(bossGroup,workGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG,1024)
                .option(ChannelOption.TCP_NODELAY, true)
                .childHandler(new ChannelInitializer<SocketChannel>()  {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(nettyHandler);
                    }
                });
        ChannelFuture future = b.bind(9993).sync();//开启需要监听 的端口
        /*ChannelFuture future1 = b.bind(9994).sync();
        ChannelFuture future2 = b.bind(9995).sync();//开启需要监听 的端口
        ChannelFuture future3 = b.bind(9996).sync();
        ChannelFuture future4 = b.bind(9997).sync();//开启需要监听 的端口
        ChannelFuture future5 = b.bind(9998).sync();
        ChannelFuture future6 = b.bind(9999).sync();//开启需要监听 的端口
        ChannelFuture future7 = b.bind(10000).sync();
        ChannelFuture future8 = b.bind(10001).sync();//开启需要监听 的端口
        ChannelFuture future9 = b.bind(10002).sync();*/
        if (future.isSuccess()) {
        }
        /*if (future1.isSuccess()) {
            log.info("开启监听 9994 ");
        }
        if (future2.isSuccess()) {
            log.info("开启监听 9995 ");
        }
        if (future3.isSuccess()) {
            log.info("开启监听 9996 ");
        }
        if (future4.isSuccess()) {
            log.info("开启监听 9997 ");
        }
        if (future5.isSuccess()) {
            log.info("开启监听 9998 ");
        }
        if (future6.isSuccess()) {
            log.info("开启监听 9999 ");
        }
        if (future7.isSuccess()) {
            log.info("开启监听 10000 ");
        }
        if (future8.isSuccess()) {
            log.info("开启监听 10001 ");
        }
        if (future9.isSuccess()) {
            log.info("开启监听 10002 ");
        }*/

    }

    /**
     * 销毁
     */
    @PreDestroy
    public void destroy() {
        bossGroup.shutdownGracefully().syncUninterruptibly();
        workGroup.shutdownGracefully().syncUninterruptibly();
    }
}

